package com.example.stumanager.config;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.stumanager.HomepageLoginInterface;
import com.example.stumanager.R;
import com.example.stumanager.database.UserSql;
import com.example.stumanager.entity.UserEntity;
import com.example.stumanager.student.StudentHomePage;

/**
 * 修改密码
 */
public class ChangePassword extends AppCompatActivity {
    Button button_change_password_ok,re;
    EditText et_c_oldpassword,et_c_confirm_newpassword,et_c_confirm_newpassword_ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_password);
        //账号
        String stuId = HomepageLoginInterface.stuId;
        //旧密码
        et_c_oldpassword=findViewById(R.id.oldpassword);
        //新密码
        et_c_confirm_newpassword=findViewById(R.id.newpassword);
        //确认密码
        et_c_confirm_newpassword_ok=findViewById(R.id.newpassword_ok);
        //确认
        button_change_password_ok=findViewById(R.id.button_change_password_ok);
        re = findViewById(R.id.re);
        re.setOnClickListener(view -> finish());
        UserSql db=new UserSql(this);

        button_change_password_ok.setOnClickListener(view -> {
            if(et_c_oldpassword.getText().toString().equals("") || et_c_confirm_newpassword.getText().toString().equals("") || et_c_confirm_newpassword_ok.getText().toString().equals("") ){
                Toast.makeText(ChangePassword.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                return ;
            }
            if (!et_c_confirm_newpassword.getText().toString().equals(et_c_confirm_newpassword_ok.getText().toString())){
                Toast.makeText(ChangePassword.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                return ;
            }
            UserEntity u=new UserEntity( stuId,  "",  et_c_confirm_newpassword.getText().toString(),  "",  "");
            if(db.change(u)){
                Toast.makeText(ChangePassword.this, "修改成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(ChangePassword.this , HomepageLoginInterface.class);
                startActivity(intent);
            }else{
                Toast.makeText(ChangePassword.this, "修改失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}